package com.qh.yyxt.service.Impl;

import com.qh.yyxt.entity.*;
import com.qh.yyxt.mapper.*;
import com.qh.yyxt.pojo.FlagDTO;
import com.qh.yyxt.pojo.entity.thing.Thing;
import com.qh.yyxt.service.FlagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class FlagServiceImpl implements FlagService
{

    @Autowired
    private FlagMapper flagMapper;
    @Autowired
    private BorrowThingMapper borrowThingMapper;
    @Autowired
    private ThingMapper thingMapper;
    @Autowired
    private GetAndReturnMapper getAndReturnMapper;
    @Autowired
    private DelayThingMapper delayThingMapper;
    @Override
    public int addFlag(Flag flag)
    {
        if(flag!=null)
            return flagMapper.addFlag(flag);
        return 0;
    }

    @Override
    public int updateInfoById(Integer id, Date passTime, String approver,Date returntime, String state)
    {
        if(id!=null&&state!=null)
        {
            if(state.equals("已归还"))
            {
                DelayThing delayThing=delayThingMapper.findByFid(id);
                if(delayThing!=null)

                {
                    delayThing.setDelaystate(state);
                    delayThingMapper.update(delayThing);
                }
            }
            return flagMapper.updateInfoById(id, passTime, approver, returntime, state);
        }
        return 0;
    }
    @Override
    public FlagDTO findById(Integer id)
    {
        FlagDTO flagDTO=new FlagDTO();
        Flag flag=flagMapper.findById(id);
        flagDTO.setFlagDTO(flag,getThings(id),getAndReturnMapper.findByFid(flag.getId()));
        return flagDTO;
    }

    @Override
    public List<FlagDTO> findByState(String state)
    {
            if(state!=null)
                return setData(flagMapper.findByState(state));
            return null;
    }

    @Override
    public List<FlagDTO> findAll() {
            return setData(flagMapper.findAll());
    }

    @Override
    public List<FlagDTO> findByUid(Integer uId) {
        if(uId!=null)
        return setDelayThingData(flagMapper.findByUid(uId));
        return null;
    }
    @Override
    public Boolean findByFIdAndUid(Integer fId, Integer uId)
    {
         if(fId!=null&&uId!=null)
         if(flagMapper.findByIdAndUid(fId,uId)!=null)
         {
             return true;
         }
        return false;
    }

    @Override
    public List<FlagDTO> findByTwoState(String state1, String state2)
    {
        List<Flag>flagList=flagMapper.findByTwoState(state1,state2);
        return setData(flagList);
    }

    public String getThings(Integer fid)
    {
        List<BorrowThing>borrowList=borrowThingMapper.findByFid(fid);
        //System.out.println(borrowList.size());
        String s="";
        List<Integer>list=new ArrayList<>();
        for(BorrowThing borrowThing:borrowList)
        {
            list.add(borrowThing.getTid());
        }
       // System.out.println(list.size());
        Set set = new HashSet(list);
        list = new ArrayList<>(set);
       // System.out.println(list.size());
        for(Integer tid:list)
        {  //System.out.println(tid);
            Thing thing=thingMapper.findById(tid);
           // System.out.println(thing);
            s+=thing.getName()+" ";
        }
        return s;
    }
    public List<FlagDTO> setData(List<Flag>list)
    {
        List<FlagDTO>dtoList=new ArrayList<>();
        for(Flag flag:list)
        {
            FlagDTO flagDTO=new FlagDTO();
            flagDTO.setFlagDTO(flag,null,getAndReturnMapper.findByFid(flag.getId()));
            dtoList.add(flagDTO);
        }
        return dtoList;
    }
    public List<FlagDTO> setDelayThingData(List<Flag>list)
    {
        List<FlagDTO>dtoList=new ArrayList<>();
        for(Flag flag:list)
        {
            FlagDTO flagDTO=new FlagDTO();
            flagDTO.setFlagDTO(flag,null,getAndReturnMapper.findByFid(flag.getId()));
            DelayThing delayThing=delayThingMapper.findByFid(flag.getId());
            if(delayThing!=null)
            {
                flagDTO.setDelayThing(delayThing);
                if(delayThing.getDelaystate().equals("审批通过")) {
                    flagDTO.setDelayThingInfo();
                }else if(delayThing.getDelaystate().equals("未通过")){
                    flagDTO.setState("延时未通过");
                }else {
                    flagDTO.setState("延时待审批");
                }
            }
            dtoList.add(flagDTO);
        }
        return dtoList;
    }
}
